package com.hengpeng.itfinoss.service.borrowInfo.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.persist.BorrowInfo;
import com.hengpeng.itfinbase.persist.BorrowInfo.BorrowStatus;
import com.hengpeng.itfinbase.persist.InvestInfo;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.service.borrowInfo.BorrowInfoService;

@Lazy
@Service
public class BorrowInfoServiceImpl implements BorrowInfoService
{
	@Autowired
	private ServiceFactory serviceFactory;

	@Override
	public ServiceResult<List<BorrowInfo>> queryList(String borrowName, String borrowUserName, Date start, Date end,
			BorrowStatus[] status, Integer pageIndex, Integer pageSize)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class);
		if (!StringHelper.isEmpty(borrowName))
		{
			query.like("borrowName", "%" + borrowName + "%");
		}
		if (!StringHelper.isEmpty(borrowUserName))
		{
			query.eq("borrowUserName", borrowUserName);
		}
		if (status != null)
		{
			query.inArray("status", status);
		}
		if (start != null)
		{
			query.ge("createTime", start);
		}
		if (end != null)
		{
			query.le("createTime", end);
		}
		query.desc("createTime");
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		return new ServiceResult<List<BorrowInfo>>(serviceFactory.getPersistService().find(BorrowInfo.class, query));
	}

	@Override
	public ServiceResult<Long> queryCount(String borrowName, String borrowUserName, Date start, Date end,
			BorrowStatus[] status)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class);
		if (!StringHelper.isEmpty(borrowName))
		{
			query.like("borrowName", "%" + borrowName + "%");
		}
		if (!StringHelper.isEmpty(borrowUserName))
		{
			query.eq("borrowUserName", borrowUserName);
		}
		if (status != null)
		{
			query.inArray("status", status);
		}
		if (start != null)
		{
			query.ge("createTime", start);
		}
		if (end != null)
		{
			query.le("createTime", end);
		}
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(query, null));
	}

	@Override
	public ServiceResult<List<InvestInfo>> queryInvestList(Long borrowId, Integer pageIndex, Integer pageSize)
	{
		DynamicQuery query = new DynamicQuery(InvestInfo.class);
		query.eq("borrowInfoId", borrowId);
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		return new ServiceResult<List<InvestInfo>>(serviceFactory.getPersistService().find(InvestInfo.class, query));
	}

	@Override
	public ServiceResult<Long> queryInvestCount(Long borrowId)
	{
		DynamicQuery query = new DynamicQuery(InvestInfo.class);
		query.eq("borrowInfoId", borrowId);
		return new ServiceResult<Long>(serviceFactory.getPersistService().findPageCount(query, null));
	}

	@Override
	public BorrowInfo getBorrowInfo(Long id, BorrowStatus status)
	{
		DynamicQuery query = new DynamicQuery(BorrowInfo.class);
		query.eq("id", id);
		if (status != null)
		{
			query.eq("status", status);
		}
		List<BorrowInfo> list = serviceFactory.getPersistService().find(BorrowInfo.class, query);
		return list == null || list.size() < 1 ? null : list.get(0);
	}
}
